<?
if( !$PARAPARA || !$PARAPARA->is_admin() )
    return;

$uid = $_GET['uid'];
$dbl = $PARAPARA->dblayer;


//- functions ----------------------------------------
function state_select( $acc ) {
    $state = $acc->state;
    $states = array( 'CREATED', 'ACTIVE', 'SUSPENDED', 'CLOSED' );

    $retval  = '<select name="accstate" >' . "\n";
    foreach($states as $s) {
        $selected = ($s == $state)? 'selected="1"' : '';
        $retval .= "<option value=\"$s\" $selected>$s</option>\n";
    }

    $retval .= "</select>\n";

    return $retval;
}


function echo_info( $info_text ) {
    echo '<div class="padm_info">'; 
    echo $info_text;
    echo "</div>\n";
}


function echo_accrow( $label, $rowhtml ) {
    echo "<tr>";
    echo '<td class="padm_acc_edit_label">' . $label . "</td>\n";
    echo '<td class="padm_acc_edit_val">' . $rowhtml . "</td>\n";
    echo "</tr>";
}


function echo_accountform( $acc ) {
    $uid = $acc->uid;
    echo '<form action="?sec=acc&ssec=edit&do=save&uid=' .$uid . '" method="post">' . "\n";
    echo '<input type="hidden" value="1" name="formdata" />' . "\n";
    echo '<table class="padm_acc_edit">' . "\n";

    echo_accrow( 'Screen name:', htmlspecialchars( $acc->screen_name ) );
    echo_accrow( 'E-mail:', htmlspecialchars( $acc->email) );
    echo_accrow( 'Created:', $acc->createdate );
    echo_accrow( 'Last change:', $acc->changedate );
    echo_accrow( 'State:',  state_select( $acc ) );
    echo_accrow( 'Karma:',  '<input class="text" name="acckarma" size="4" value="' . $acc->karma . '"/>');
    echo_accrow( "Admin's comment:", 
                 '<textarea class="text" name ="acccomment" cols="40" rows="5">'.
                 htmlspecialchars($acc->comment) .'</textarea>');
    echo "<tr>\n";
    echo '<td class="padm_acc_edit_buttons" colspan="2">' . "\n";
    echo '<input class="button" type="submit" name="accsave" value="Save"/>' ."\n";
    echo '<input class="button" type="submit" name="accdel" value="Delete"/>' ."\n";
    echo '<input class="button" type="submit" name="accres" value="Reset"/>' ."\n";
    echo "</td>\n</tr>\n";
    echo "</table>\n";
    echo "</form>\n";
}

//- logic --------------------------------------------
if( $_POST['accdel'] ) {
    $dbl->account_delete_forever( $uid );
    echo_info('Account deleted forever');
    return;
}


$acc = $dbl->account_fetch_by_uid( $uid );

if( $_POST['accsave'] ) {
    // fix up the account

    $need_save = false;

    // check every possible param 
    if($_POST['accstate'] != $acc->state) {
        $acc->state = $_POST['accstate'];
        $need_save  = true;
    }

    if(is_numeric($_POST['acckarma']) && ($_POST['acckarma'] != $acc->karma) ) {
        $acc->karma = $_POST['acckarma'];
        $need_save  = true;
    }

    if($_POST['acccomment'] != $acc->comment ) {
        $acc->comment = $_POST['acccomment'];
        $need_save    = true;
    }


    if($need_save) {
        $save_status = $dbl->account_save( $acc );
        if($save_status) {
            echo_info("Account data saved");
            $acc = $dbl->account_fetch_by_uid( $uid );
        }
        else
            echo_info("Account not saved");
    }
    else
        echo_info("Nothing changed");
}



?>
<h2>Account editor</h2>

<?

if(!$acc) {
    echo_info( 'Account not found');
    return;
}

echo_accountform( $acc );

?>
